package com.example.mybaties.testone.bean_process;

import com.example.mybaties.testone.domain.Car;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

public class MyBeanPostProcessor implements BeanPostProcessor {

    private Logger logger = LogManager.getLogger(this.getClass());

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if("car".equals(beanName)){
            Car car = (Car) bean;
            if(car.getColor() == null){
                car.setColor("blue");
            }
            logger.debug("MyBeanPostProcessor..........postProcessBeforeInitialization()");
        }
        return null;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if("car".equals(beanName)){
            Car car = (Car) bean;
            if(car.getMaxSpeed() == 0){
                car.setMaxSpeed(250);
            }
            logger.debug("MyBeanPostProcessor..........postProcessAfterInitialization()");
        }
        return null;
    }
}
